/*
 * @Author: zhangxuanjiu 
 * @Date: 2017-08-31 10:29:41 
 * @Last Modified by: zhangxuanjiu
 * @Last Modified time: 2018-04-09 11:04:26
 */

const path = require('path')
const webpack = require('webpack')
var OpenBrowserPlugin = require('open-browser-webpack-plugin')
const serverConfig = require('./serverConfig.js')
const HtmlWebpackPlugin = require('html-webpack-plugin')

var webpackConfig = {
  resolve: {
    modules: [
      path.join(__dirname, 'src'),
      'node_modules'
    ],
    extensions: ['.js', '.jsx', '.css', '.less', 'jsx', '.ts']
  },
  context: path.join(__dirname, 'src'),
  entry: {
    main: [
      './main.jsx',
      `webpack-dev-server/client?http://${serverConfig.host}:${serverConfig.port}/`,
      'webpack/hot/dev-server',
    ]
  },
  output: {
    path: path.join(__dirname, 'build'),
    filename: '[name].js',
    publicPath: './',
    chunkFilename: '[name].js?[hash:8]'
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"development"'
      },
      env: JSON.stringify('dev')
    }),
    new OpenBrowserPlugin({ url: `http://${serverConfig.host}:${serverConfig.port}` }),
    new HtmlWebpackPlugin({
      // title: 'rd平台',
      template: 'index.html', // 源模板文件
      filename: './index.html', // 输出文件【注意：这里的根路径是module.exports.output.path】
      showErrors: true,
      inject: 'body',
      hash: true
      // chunks: ["common",'index']
    })
  ],
  module: {
    rules: [{
      test: /(\.js)|(\.jsx)$/,
      loader: 'eslint-loader',
      enforce: 'pre',
      options: {
        formatter: require('eslint-friendly-formatter')
      }
    },
    {
      test: /(\.js)|(\.jsx)$/,
      use: ['babel-loader'],
      exclude: /node_modules/
    },
    {
      test: /\.ts?$/,
      loader: 'ts-loader',
      exclude: /node_modules/,
    },
    {
      test: /\.css/,
      // use: ['style-loader', 'css-loader', 'postcss-loader'],
      use: ['style-loader', { loader: 'css-loader', options: { modules: 1 } }]
    },
    {
      test: /\.less/,
      use: ['style-loader', 'css-loader', 'postcss-loader', 'less-loader']
    },
    {
      test: /\.(png|jpg|gif|svg|eot|svg|ttf|woff|woff2)$/,
      loader: 'file-loader',
      options: {
        name: 'img/styleImg/[name].[ext]'
      }
    }
    ],
  }
}

module.exports = webpackConfig